package ru.innoconsulting.building;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class BuildingMaterialsCalc extends Application {

	private Panel panel(Component content) {
		Panel p = new Panel();
		p.addComponent(content);
		return p;
	}

	IndexedContainer materialsData = new IndexedContainer() {
		{
			String[] fields = { "Label", "m2", "m3", "w", "h", "l" };
			for (String p : fields) {
				addContainerProperty(p, String.class, "");
			}

			for (int i = 0; i < 10; i++) {
				Object id = addItem();
				getContainerProperty(id, "Label").setValue("Material1");
				getContainerProperty(id, "l").setValue("4");
			}
		}
	};

	Container navigationData = new IndexedContainer() {
		{
			addContainerProperty("Label", String.class, "");
			addContainerProperty("id", String.class, "");

			Object id = addItem();
			getContainerProperty(id, "Label").setValue("Calc");
			getContainerProperty(id, "id").setValue("0");
		}
	};

	Panel navigation = panel(new GridLayout(1, 1) {
		{
			addComponent(new Label("Calculator"));
			addComponent(new Label("Preferences"));
			addComponent(new Label("Materials"));
		}
	});

	Component header = panel(new Label("Header"));

	Table materialsTable = new Table("Materials", materialsData);
	Panel workspace = panel(materialsTable);

	Component footer = panel(new Label("Footer"));
	TabSheet tabsheet = new TabSheet();

	private HorizontalLayout horisontal(Component... components) {

		HorizontalLayout h = new HorizontalLayout();
		h.setWidth(Sizeable.SIZE_UNDEFINED, 0);
		for (Component component : components) {
			h.addComponent(component);
		}
		return h;
	}

	private VerticalLayout vertical(Component... components) {
		VerticalLayout h = new VerticalLayout();
		for (Component component : components) {
			h.addComponent(component);
		}
		return h;
	}

	int COL_0 = 0;
	int ROW_0 = 0;
	int COL_1 = 1;
	int ROW_1 = 1;
	int ROW_2 = 2;
	float RATIO_TWO_THIRDS = 0.66f;

	@Override
	public void init() {
		setTheme("reindeer");
		setMainWindow(new Window("Building materials calc", createAppLayout()));
	}

	private ComponentContainer createAppLayout() {
		// Our main layout is a horiozontal layout
		HorizontalLayout main = new HorizontalLayout();
		main.setMargin(true);
		main.setSpacing(true);

		main.addComponent(navigation);
		navigation.setSizeFull();

		// vertically divide the right area
		VerticalLayout workList = new VerticalLayout();
		workList.setSpacing(true);
		main.addComponent(workList);

		// table on top

		materialsTable.setWidth("500px");
		workList.addComponent(panel(materialsTable));

		// Label on bottom
		Panel workSpace = panel(new Label(
				"aaa aaa aaa aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaa"
						+ " aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaa aaaaaaa"
						+ "a  aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaa"
						+ "aaaaaaaaaaaaaaaaaaaaaaaa"
						+ "aaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa"));
		workSpace.setWidth("500px"); // some limit is good for text
		workList.addComponent(workSpace);

		AppScreen app = new AppScreen().setHeader(header).setFooter(footer)
				.setNavigation(navigation)
				.setMainView(vertical(workList, workSpace));

		return main;

	}

	public ComponentContainer createAppLayout_() {
		VerticalLayout l = new VerticalLayout();
		// Create a grid layout
		// setTheme("runo");
		final GridLayout grid = new GridLayout(3, 3);
		grid.setHeight("80%");
		grid.setWidth("100%");
		grid.setSpacing(true);
		// The style allows us to visualize the cell borders in this example.
		// grid.addStyleName("gridexample");

		// grid.setWidth(400, Sizeable.UNITS_PIXELS);
		// grid.setHeight(400, Sizeable.UNITS_PIXELS);\

		// First we insert four components that occupy one cell each
		grid.addComponent(header, COL_0, ROW_0, COL_1, ROW_0);
		grid.setComponentAlignment(header, Alignment.MIDDLE_CENTER);

		grid.addComponent(footer, COL_0, ROW_2, COL_1, ROW_2);
		grid.setComponentAlignment(footer, Alignment.MIDDLE_CENTER);

		grid.addComponent(navigation, COL_0, ROW_1);
		grid.setComponentAlignment(navigation, Alignment.MIDDLE_CENTER);
		navigation.setWidth("300px");
		navigation.setHeight("100%");

		grid.addComponent(workspace, COL_1, ROW_1);
		grid.setComponentAlignment(workspace, Alignment.MIDDLE_CENTER);

		// Add the layout to the containing layout.
		l.addComponent(grid);
		navigation.setWidth("30%");

		// Align the grid itself within its container layout.
		l.setComponentAlignment(grid, Alignment.MIDDLE_CENTER);
		return l;
	}
}
